package ano.it.controller;

import ano.it.constant.MessageConstant;
import ano.it.entity.PageResult;
import ano.it.entity.QueryPageBean;
import ano.it.entity.Result;
import ano.it.pojo.Device;
import ano.it.pojo.Role;
import ano.it.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roles=roleService.findAll();
            return new Result(true, MessageConstant.QUERY_DATA_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_DATA_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=roleService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/addPermission")
    public Result addPermission(Integer roleId,Integer permissionIds[]){
        try {
            roleService.addPermission(permissionIds,roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_POWER_FAIL);
        }
        return new Result(false,MessageConstant.ADD_POWER_SUCCESS);

    }
    @RequestMapping("/findPermissionIdsByUserId")
    public Result findPermissionIdsByUserId(Integer id){
        try {
            List<Integer> list=roleService.findPermissionIdsByUserId(id);
            for (Integer integer : list) {
                System.out.println("fdsafsda"+integer);
            }
            return new Result(false,MessageConstant.QUERY_DATA_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DATA_FAIL);
        }


    }

}
